#!/usr/bin/env python

from celtuce.accessor import Accessor

import time

from celtuce.help import logging
logging.getLogger().setLevel(logging.LEVEL_DEBUG)

URL = 'tcp://127.0.0.1:14300'

TABLE = 'table_test'

def runTest() :
    accessor = Accessor( URL )
    try :
        accessor.insert(TABLE,'hello', 'world')
        key = 'hello-not-found'
        response = accessor.find(TABLE, key)
        assert response.isOk()
        _key,_value = response.body
        assert _key == key
        assert _value is None

        response = accessor.find(TABLE, 'hello')
        assert response.isOk()
        _key,_value = response.body
        assert _key == 'hello'
        assert _value == 'world'

        # test performance
        test_times = 1000
        print 'test find %d times ...' % (test_times,)
        time_start = time.time()
        for i in xrange(test_times) :
            response = accessor.find(TABLE, 'hello')
            assert response.isOk()
            _key,_value = response.body
            assert _key == 'hello'
            assert _value == 'world'
        time_last = time.time() - time_start
        tps = test_times / time_last
        print 'test find() %d times last %.2f seconds, tps=%.2f' % (test_times, time_last, tps)

    finally :
        accessor.shutdown()

    ## runTest()

if  __name__ == '__main__' :
    runTest()

